package com.example.arithmeticleetcode.leetcode.September;


import java.util.ArrayList;
import java.util.List;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-09-11 09:29
 **/
public class Demo10 {
    public static void main(String[] args) {
        System.out.println((int)'a');
        System.out.println((int)'z');
        System.out.println((int)'A');
        System.out.println((int)'Z');

        System.out.println(reverseOnlyLetters("ab-cd"));
        System.out.println(reverseOnlyLetters("a-bC-dEf-ghIj"));
        System.out.println(reverseOnlyLetters("Test1ng-Leet=code-Q!"));
    }

    public static String reverseOnlyLetters(String S) {
        StringBuilder sb = new StringBuilder(S);
        String reverseS = sb.reverse().toString();
        List<Character> list = new ArrayList<>();
        for(int i = 0, len = reverseS.length(); i < len; i++) {
            if (isChar(reverseS.charAt(i))) {
                list.add(reverseS.charAt(i));
            }
        }
        System.out.println(list);
        StringBuilder stringBuilder = new StringBuilder();
        int listIndex = 0;
        for(int i = 0, len = S.length(); i < len; i++) {
            if (isChar(S.charAt(i))) {
                stringBuilder.append(list.get(listIndex++));
            } else {
                stringBuilder.append(S.charAt(i));
            }
        }
        return stringBuilder.toString();
    }

    private static boolean isChar(char c) {
        return (c >= 65 && c <= 90) || (c >= 97 && c <= 122);
    }
}
